package com.jack.blog.service.serviceImpl;

import com.jack.blog.dao.CategoryMapper;
import com.jack.blog.dao.BlogMapper;
import com.jack.blog.dao.MessageMapper;
import com.jack.blog.dto.BlogDto;
import com.jack.blog.entity.Blog;
import com.jack.blog.entity.Message;
import com.jack.blog.service.BlogService;
import com.jack.blog.utils.*;
import com.jack.blog.vo.info.BlogInfo;
import com.jack.blog.vo.BlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

@Service
public class BlogServiceImpl implements BlogService {
    @Value("#{config.pageSize}")
    private int PAGE_SIZE;
    @Value("#{config.blogDirectory}")
    private File BLOG_DIRECTORY;
    @Value("#{config.coverDirectory}")
    private File COVER_DIRECTORY;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BlogDto blogDto;

    @Override
    public List<BlogVo> getBlogs(int uid,int page) {
        int begin=(page-1)* this.PAGE_SIZE;
        int end=page* this.PAGE_SIZE;
        List<Blog> blogs;
        if (page==0)
            blogs = blogMapper.queryBlogsByUid(uid,0,Integer.MAX_VALUE);
        else blogs = blogMapper.queryBlogsByUid(uid, begin, end);
        return blogDto.BlogToBlogVo(blogs);
    }

    @Override
    public List<BlogVo> getBlogsByCid(int cid, int page) {
        int begin=(page-1)* this.PAGE_SIZE;
        int end=page* this.PAGE_SIZE;
        List<Blog> blogs;
        if (page==0)
            blogs=blogMapper.queryBlogsByCid(cid,0,Integer.MAX_VALUE);
        else blogs=blogMapper.queryBlogsByCid(cid, begin, end);
        return blogDto.BlogToBlogVo(blogs);
    }

    @Override
    public List<BlogVo> getBlogsByTitle(int uid,int page,String title) {
        int begin=(page-1)* this.PAGE_SIZE;
        int end=page* this.PAGE_SIZE;
        List<Blog> blogs;
        if (page==0)
            blogs=blogMapper.queryBlogsByTitle(uid,0,Integer.MAX_VALUE,title);
        else blogs=blogMapper.queryBlogsByTitle(uid, begin, end, title);
        return blogDto.BlogToBlogVo(blogs);
    }

    @Override
    public BlogVo getBlog(int blogId) {
        Blog blog = blogMapper.queryBlogById(blogId);
        return blogDto.BlogToBlogVo(blog);
    }

    @Override
    @Transactional
    public int addBlog(int uid, BlogInfo blogInfo) throws Exception{
        int cid = blogInfo.getCid();
        Blog blog = blogDto.BlogCreateToBlog(uid, blogInfo);
        String cover = blog.getCover();
        String html = blog.getHtml();
        categoryMapper.updateBlogCategory(cid,null,null,1);
        blogMapper.insertBlog(blog);
        loadCover(cover, blogInfo.getImg());
        loadBlog(html, blogInfo.getMd());
        return blog.getId();
    }

    @Override
    @Transactional
    public void updateBlogInfo(int blogId, BlogInfo blogInfo) throws Exception {
        MultipartFile img = blogInfo.getImg();
        MultipartFile md = blogInfo.getMd();
        Blog old = blogMapper.queryBlogById(blogId);
        Blog update = blogDto.BlogUpdateToBlog(blogInfo);
        int cid = update.getCid();
        int oldCid=old.getCid();
        if (cid!=oldCid){
            categoryMapper.updateBlogCategory(oldCid,null,null,-1);
            categoryMapper.updateBlogCategory(cid,null,null,1);
        }
        blogMapper.updateBlogInfo(blogId,update.getTitle(), update.getBlogAbstract(), cid,update.getCover(),update.getHtml());
        if (update.getCover()!=null) {
            unloadCover(old.getCover());
            loadCover(update.getCover(), img);
        }
        if (update.getHtml()!=null) {
            unloadBlog(old.getHtml());
            loadBlog(update.getHtml(), md);
        }
    }

    @Override
    @Transactional
    public void deleteBlog(int blogId) {
        Blog blog = blogMapper.queryBlogById(blogId);
        int cid = blog.getCid();
        categoryMapper.updateBlogCategory(cid,null,null,-1);
        List<Message> list = messageMapper.queryMessages(blogId);
        for (Message message:list)
            messageMapper.deleteMessage(message.getId());
        blogMapper.deleteBlogById(blogId);
        unloadCover(blog.getCover());
        unloadBlog(blog.getHtml());
    }

//    @Override
//    public void addBrowse(int blogId) {
//        blogMapper.addBlogData(blogId,1,0,0,0);
//    }
//
//    @Override
//    public void addGood( int blogId) {
//        blogMapper.addBlogData(blogId,0,1,0,0);
//    }
//
//    @Override
//    public void addBad(int blogId) {
//        blogMapper.addBlogData(blogId,0,0,1,0);
//    }

    private void loadCover(String cover,MultipartFile img)throws Exception{
        File file=new File(COVER_DIRECTORY,cover);
        FileUtil.checkFile(file);
        img.transferTo(file);
    }

    private void loadBlog(String html,MultipartFile blog)throws Exception{
        File file=new File(BLOG_DIRECTORY,html);
        FileUtil.checkFile(file);
        MdUtil.mdToHtml(blog.getInputStream(),new FileOutputStream(file));
    }

    private void unloadCover(String cover){
        File file=new File(COVER_DIRECTORY,cover);
        FileUtil.deleteFile(file);
    }

    private void unloadBlog(String html){
        File file=new File(BLOG_DIRECTORY,html);
        FileUtil.deleteFile(file);
    }
}
